//
//  TravelManager.cpp
//  SpaceTrader
//
//  Created by Brandon Chastain on 11/4/12.
//  Copyright (c) 2012 The Hex Pistols. All rights reserved.
//

#include "TravelManager.h"

TravelManager::TravelManager() { }

int TravelManager::getDistance(Planet *A, Planet *B, Universe *universe)
{
	std::vector<System*> systems = universe->getSystems();
    System *aSystem, *bSystem;
	
	/* set aSystem and bSystem to their respective systems*/
	for(unsigned int i =0; i < systems.size(); i++) {
		std::vector<Planet*> planets = systems[i]->getPlanets();
		
		for(unsigned int j = 0; j< planets.size(); j++) {
			if(planets[j]->getName() == A->getName()) aSystem = systems[i];
			if(planets[j]->getName() == B->getName()) bSystem = systems[i];
		}
	}
	
	/*Calculate Distance relative to System planets appear in*/
    if (aSystem == bSystem) {
        int diffX = B->getX() - A->getX(),
			diffY = B->getY() - A->getY();
		
        return pow(pow(diffX, 2) + pow(diffY, 2), .5)/10;		
    } else {
		
        int diffSystemXs = bSystem->getX() - aSystem->getX(),
			diffSystemYs = bSystem->getY() - aSystem->getY(),
			planetBX = B->getX(),
			planetBY = B->getY(),
			planetAX = A->getX(),
			planetAY = A->getY();
        return pow(pow(diffSystemXs, 2) + pow(diffSystemYs, 2), .5) + pow(pow(planetBX, 2) + pow(planetBY, 2), .5)/10
                    + pow(pow(planetAX, 2) + pow(planetAY, 2), .5)/ 10;
    }
}
